/************************************************************************************************************
* File Name:SendCmdToDev.h
* Abstract :send command to camera
* 
* Version :1.0
* Author  :Liu Tengfei
* Date    :2013.12.1
*
* Shanghai Ruishi Machine Vision Technology Co., Ltd.
* All rights reserved.
*************************************************************************************************************/
#ifndef CMDTODEV_H
#define CMDTODEV_H
#define SOCKET_TYPE	int

//maxim log in user name length
#define USER_NAME_MAX_LENGTH			(128)
#define USER_PASSWORD_MAX_LENGTH		(64)

//command for logging in
#define CMD_LOGIN_REQUEST				(0x00000003)
#define CMD_LOGIN_ANSWER				(0x00000004)

//Encryption method
#define ENCRYPTION_NO					(0)
#define ENCRYPTION_128BIT_MD5			(1)

//log in error code 
#define LOGIN_SUCCESS					(0)
#define LOGIN_FAILED					(1)
#define LOGIN_ENCRYPT_UNSUPPORTED		(2)

//send command error code
#define SEND_CMD_SUCCESS				(0)
#define SEND_CMD_FAILED					(-1)
#define SEND_CMD_LOGIN_FAILED			(-2)

//login request message structure
typedef struct LoginReqMsg
{
	DWORD		id;
	DWORD		encrypt;
	char		userName[128];
	char		password[64];
}RoseekLoginReqMsg;

//login Answer message structure
typedef struct LoginAnsMsg
{
	DWORD		id;
	DWORD		answer;
	char		msg[256];
}RoseekLoginAnsMsg;

//command head information structure
typedef struct CmdPortHeadInfo
{
	DWORD		index;
	DWORD		id;
	DWORD		method;
	DWORD		reserved[5];
}RoseekCmdPortHeadInfo;

//login back information
typedef struct CmdBackMsgHeadInfo
{
	DWORD		index;
	int			result;
	char 		msg[256];
}RoseekCmdBackMsgHeadInfo;

#define SOCKET_TYPE	int
#define CMD_DATA_BUF_SIZE (10*1024)
extern "C"
{
#include "sock.h"
}
#include <string>

//class OperateOperateOperate
class CSendCmdToDev
{
public:
	CSendCmdToDev(void);
	~CSendCmdToDev(void);
public:
	void		  SetDevAddr(const char *pIP, unsigned short port);
	int			  SendCMD(DWORD attribute, DWORD method);   
	int			  SendCMD(DWORD attribute, DWORD method, DWORD param);
	int			  SendCMD(DWORD attribute, DWORD method, const char *pParam);
	int			  SendCMD(DWORD attribute, DWORD method, const char *pParam, int size);
	DWORD		  GetBackDataLen();
	const char*   GetBackDataPtr();
	DWORD		  GetBackDataUint32();
	float         GetBackDataFloat();
	std::string   GetDevIP();
	unsigned short GetDevPort();
	DWORD		  GetCmdResult(){return m_dwResult;};
	void          Close();
	void          Reset();
	const char*	  GetBackMsgPtr();
	int			  SetLoginInfo(const char *pUserName, const char *pPassword);
	int			  Connect();
protected:
	bool          CreateAndConnect();
protected:
	SOCKET_TYPE		m_sockThis;
	DWORD			m_backDataLen;
	char		    m_pBackData[CMD_DATA_BUF_SIZE + 1];
	char		    m_pSendData[CMD_DATA_BUF_SIZE + 1];
	std::string		m_sDevIP;
	unsigned short  m_DevPort;
	bool			m_bAbort;
	char			m_pchMsg[256];
	char			*m_pchUserName;
	char			*m_pchPassword;
	DWORD			m_dwResult;
	int				LogInEncryption();
};
#endif


